package com.logisticsinquiry.android.ui.query

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.core.widget.addTextChangedListener
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer

import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.logisticsinquiry.android.QueryApplication
import com.logisticsinquiry.android.R
import com.logisticsinquiry.android.logic.database.AppDatabase
import com.logisticsinquiry.android.logic.entity.ShipperCodeResponseHistory
import kotlinx.android.synthetic.main.history.*


class QueryFragment: Fragment() {
    val viewModel by lazy { ViewModelProvider(this).get(QueryViewModel::class.java) }
    private lateinit var adapter: ShipperAdapter
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.history, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        val layoutManager = LinearLayoutManager(activity)
        val username = activity!!.intent.getStringExtra("username")?:""
        user_name.text = username
        recycler_history.layoutManager = layoutManager
        val shipperCodeResponseHistoryDao = AppDatabase.getDatabase(QueryApplication.context).shipperCodeResponseHistoryDao()
        viewModel.traceList = ArrayList(shipperCodeResponseHistoryDao.loadAllShipperCodeResponseHistorys(username))
        adapter = ShipperAdapter(this, viewModel.traceList, username)
        recycler_history.adapter = adapter
        input_query.addTextChangedListener{
                editable ->
            val content = editable.toString()
            if(content.isNotEmpty()){
                Log.v("调试信息",content)
                viewModel.searchShipper(content)
            }else {
                /*        bgImageView.visibility = View.VISIBLE*/
                viewModel.traceList.clear()
                viewModel.traceList.addAll(ArrayList(shipperCodeResponseHistoryDao.loadAllShipperCodeResponseHistorys(username)))
                adapter.notifyDataSetChanged()
            }
        }
        viewModel.searchShipperLiveData.observe(viewLifecycleOwner, Observer {
                result ->
            val traces = result.getOrNull()
            Log.v("调试信息2",traces.toString())
            if(traces!!.Shippers != null){
                if(traces!!.Shippers.isEmpty() == true ){
                    viewModel.traceList.clear()
                    Toast.makeText(activity,"未能查询到快递物流", Toast.LENGTH_SHORT).show()
                    result.exceptionOrNull()?.printStackTrace()
                }else {
                    recycler_history.visibility = View.VISIBLE
                    /* bgImageView.visibility = View.GONE*/
                    viewModel.traceList.clear()
                    val shipperResponseSearched = ShipperCodeResponseHistory(traces.LogisticCode,traces.Shippers[0].ShipperName, username)
                    viewModel.traceList.addAll(listOf(shipperResponseSearched))
                    adapter.notifyDataSetChanged()
                }
            }else {
                viewModel.traceList.clear()
                Toast.makeText(activity,"未能查询到快递物流", Toast.LENGTH_SHORT).show()
                result.exceptionOrNull()?.printStackTrace()
            }
        })
    }
}